const Api = require('./api.js');
const againLogin = require('./againLogin')
let host = Api.host
const request = {
	http: function(param) {
		return new Promise(function(resolve, reject) {
			wx.request({
				url: param.url,
				header: {
					"app-source": 2,
					"applet-num": 2,
					...param.header,

				},
				method: param.type,
				data: param.data,
				timeout: 5000,
				success: function(res) {
					if (res.data.code == 200) {
						console.log(param.url, "url")

						uni.setStorageSync('isNetwork', false)
						console.log(uni.getStorageSync('isNetwork'), '成功200')
						resolve(res.data);

					} else if (res.data.code == 403) {

						uni.removeStorageSync('token')
						againLogin.againLogin()

					} else {
						console.log(res.data.msg, '错误22')
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000
						})
					}
				},
				fail: function(error) {
					console.log(error.errMsg, '错误333')
					if (error.errMsg == 'request:fail request time out' || error.errMsg ==
						'request:fail network not available') {
						uni.setStorageSync('isNetwork', true)
						console.log(uni.getStorageSync('isNetwork'), '失败200')
					}

					// uni.showToast({
					//   title: error.errMsg,
					//   icon: 'none',
					//   duration: 2000
					// })
				}
			})
		});

	}

}

module.exports = request